home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / setup / vbnet / 17 controls / customcontroldemo / gradientbackground.vb < prev    next >
Encoding:
Text File  |  2002-03-16  |  3.1 KB  |  96 lines

  1. Imports System.ComponentModel
  2. Imports System.Drawing.Drawing2D
  3.  
  4. Public Class GradientBackground
  5.     Inherits System.Windows.Forms.Control
  6.  
  7.     '--------------------------------------------------------------
  8.     ' The StartColor property
  9.     '--------------------------------------------------------------
  10.  
  11.     Dim m_StartColor As Color = Color.Blue
  12.  
  13.     <Description("The start color for the gradient")> _
  14.     Property StartColor() As Color
  15.         Get
  16.             Return m_StartColor
  17.         End Get
  18.         Set(ByVal Value As Color)
  19.             m_StartColor = Value
  20.             ' Redraw the control when this property changes.
  21.             Me.Invalidate()
  22.         End Set
  23.     End Property
  24.  
  25.     Sub ResetStartColor()
  26.         m_StartColor = Color.Blue
  27.     End Sub
  28.  
  29.     Function ShouldSerializeStartColor() As Boolean
  30.         Return Not m_StartColor.Equals(Color.Blue)
  31.     End Function
  32.  
  33.     '--------------------------------------------------------------
  34.     ' The EndColor property
  35.     '--------------------------------------------------------------
  36.  
  37.     Dim m_EndColor As Color = Color.Black
  38.  
  39.     <Description("The end color for the gradient")> _
  40.     Property EndColor() As Color
  41.         Get
  42.             Return m_EndColor
  43.         End Get
  44.         Set(ByVal Value As Color)
  45.             m_EndColor = Value
  46.             ' Redraw the control when this property changes.
  47.             Me.Invalidate()
  48.         End Set
  49.     End Property
  50.  
  51.     Sub ResetEndColor()
  52.         m_EndColor = Color.Black
  53.     End Sub
  54.  
  55.     Function ShouldSerializeEndColor() As Boolean
  56.         Return Not m_EndColor.Equals(Color.Black)
  57.     End Function
  58.  
  59.     '--------------------------------------------------------------
  60.     ' The GradientMode property
  61.     '--------------------------------------------------------------
  62.  
  63.     Dim m_GradientMode As Drawing2D.LinearGradientMode = Drawing.Drawing2D.LinearGradientMode.ForwardDiagonal
  64.  
  65.     <Description("The gradient mode"), DefaultValue(Drawing2D.LinearGradientMode.ForwardDiagonal)> _
  66.     Overridable Property GradientMode() As Drawing2D.LinearGradientMode
  67.         Get
  68.             Return m_GradientMode
  69.         End Get
  70.         Set(ByVal Value As Drawing2D.LinearGradientMode)
  71.             m_GradientMode = Value
  72.             ' Redraw the control when this property changes.
  73.             Me.Invalidate()
  74.         End Set
  75.     End Property
  76.  
  77.     ' render the control backgound
  78.  
  79.     Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
  80.         ' let the base control do its stuff.
  81.         MyBase.OnPaint(e)
  82.         ' Create a gradient brush as large as the client area, with specified
  83.         ' start/end color and gradient mode
  84.         Dim br As New Drawing2D.LinearGradientBrush(Me.ClientRectangle, m_StartColor, m_EndColor, m_GradientMode)
  85.  
  86.         ' paint the background.
  87.         e.Graphics.FillRectangle(br, Me.ClientRectangle)
  88.         ' orderly destroy the brush.
  89.         br.Dispose()
  90.     End Sub
  91.  
  92.     Private Sub GradientBackground_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
  93.         Me.Invalidate()
  94.     End Sub
  95. End Class
  96.